package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubNotice;
import cn.pconline.autoclub.entity.User;
import cn.pconline.officialclub.entity.OClubNotice;
import cn.pconline.passport2.util.T;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author chensy
 */
public class CensorNoticeService extends CensorService {

    /**
     * 公告typeId,在bean初始化的时候赋值
     */
    private long typeIdClubNotice;
    @Autowired
    private UserService userService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private DynaService dynaService;

    public void setTypeIdClubNotice(long typeIdClubNotice) {
        this.typeIdClubNotice = typeIdClubNotice;
    }

    /**
     * 审核应用回馈
     *
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedback(String interfaceUrl, String url, int status,
            long version) {
        String result = null;
        // url must format like : http://my.pcauto.com.cn/2?typeId=203
        Map<String, Long> map = getMapByUrl(url);
        long clubId = map.get("clubId");
        long typeId = map.get("typeId");
        long dynaInfoId = map.get("dynaInfoId");
        if (typeId == typeIdClubNotice) {
            ClubNotice notice = null;
            try {
                notice = geliDao.find(ClubNotice.class, clubId);
            } catch (DataAccessException e) {

                log.info("审核平台回调公告已不存在:" + clubId);
            }
            if (notice != null && isTheSameVersion(version, notice.getUpdateAt().getTime())) {
                switch (status) {
                    //审核通过?
                    case CENSORSYSTEM_RESPONSE_PASS:
                        notice.setStatus(ClubNotice.STATUS_AUDITED);
                        this.geliDao.update(notice, "status");
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                        notice.setStatus(ClubNotice.STATUS_NOT_AUDITED);
                        notice.setContent("内容含有敏感词，已屏蔽");
                        this.geliDao.update(notice, "status,content");
                        //删除已发布的动态
                        dynaService.deleteClubDyna(clubId, dynaInfoId);
                        log.info("车友会公告审核未通过：" + clubId);
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
            }
        }

        return result;
    }
    /**
     * 品牌车友会审核应用回馈
     *
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedbackOfficialClub(String interfaceUrl, String url, int status,
            long version) {
        String result = null;
        // url must format like : http://my.pcauto.com.cn/2?typeId=203
        Map<String, Long> map = getMapByUrl(url);
        long clubId = map.get("clubId");
        long typeId = map.get("typeId");
        if (typeId == typeIdClubNotice) {
            OClubNotice notice = null;
            try {
                notice = geliDao.find(OClubNotice.class, clubId);
            } catch (DataAccessException e) {

                log.info("审核平台回调公告已不存在:" + clubId);
            }
            if (notice != null && isTheSameVersion(version, notice.getUpdateAt().getTime())) {
                switch (status) {
                    //审核通过?
                    case CENSORSYSTEM_RESPONSE_PASS:
                        notice.setStatus(ClubNotice.STATUS_AUDITED);
                        this.geliDao.update(notice, "status");
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                        notice.setStatus(ClubNotice.STATUS_NOT_AUDITED);
                        notice.setContent("内容含有敏感词，已屏蔽");
                        this.geliDao.update(notice, "status,content");
                        log.info("品牌车友会公告审核未通过：" + clubId);
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
            }
        }

        return result;
    }
    /**
     * 创建公告时,首次通知审核平台新建公告状态
     *
     * @param request
     * @param notice
     * @param dynaInfoId
     */
    public void notifyCensorSystemCreate(HttpServletRequest request, final ClubNotice notice, final long dynaInfoId) {
        if (csr_switch) {
            User user = userService.findUser(notice.getUpdateBy());
            final Map<String, Object> params = convert4Create(request, user, notice.getClubId(),
                    typeIdClubNotice, dynaInfoId, notice.getContent(), notice.getUpdateAt().getTime(), true,CLUB_TYPE_COMMON);
            executorService.submit(new CensorCall(receiveHome, params));
        }
    }
     /**
     * 品牌车友会创建公告时,首次通知审核平台新建公告状态
     *
     * @param request
     * @param notice
     * @param dynaInfoId
     */
    public void notifyCensorSystemCreate(HttpServletRequest request, final OClubNotice notice) {
        if (csr_switch) {
            User user = userService.findUser(notice.getUpdateBy());
            final Map<String, Object> params = convert4Create(request, user, notice.getoClubId(),
                    typeIdClubNotice, 0, notice.getContent(), notice.getUpdateAt().getTime(), true,CLUB_TYPE_OFFICIAL);
            executorService.submit(new CensorCall(receiveHome, params));
        }
    }

    /**
     * 将ClubNotice类转换成向审核系统传递的参数
     *
     * @param request
     * @param user
     * @param id
     * @param typeId
     * @param content
     * @param isNew
     * @param time
     * @return
     */
    protected Map<String, Object> convert4Create(HttpServletRequest request, User user,
            long clubId, long typeId, long dynaInfoId, String content, long time, boolean isNew,int type) {
        Map<String, Object> m = this.convert4Create(request, user, clubId, typeId, content, time, isNew,type);
        Club club = clubService.getClub(clubId);
        String root = "http://" + T.stringValue(request.getHeader("host"), "") + T.stringValue(request.getContextPath(), "");
        m.put(URI, root + "/" + club.getClubUrl() + "/?typeId=" + typeId + "&clubId=" + clubId + "&dynaInfoId=" + dynaInfoId+"&clubType="+type);
        return m;
    }

    @Override
    public boolean isCorrectFeedback(String url) {
        Map<String, Long> map = getMapByUrl(url);
        long typeId = 0;
        if (map != null && !map.isEmpty()) {
            typeId = map.get("typeId");
        }
        return typeIdClubNotice == typeId;
    }
}
